package com.jinbei.restController;

import com.github.pagehelper.PageInfo;
import com.jinbei.common.annotation.AuthorAnnotation;
import com.jinbei.common.base.ResponseBase;
import com.jinbei.common.base.ResponseBaseEntity;
import com.jinbei.domain.Logs;
import com.jinbei.service.LogsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zflu
 * 日志 restful 接口
 * Created by lucky on 2018/7/10.
 */
@RestController
@RequestMapping("/logs")
public class LogsController {

    private final Logger logger = LoggerFactory.getLogger(LogsController.class);

    @Autowired
    private LogsService logsService;

    /**
     * @author zflu
     * @Description   根据日志id查询日志
     * @date 2018/7/10 16:44
     * @param id
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/logs/queryLogs" ,logs = "日志查询")
    @GetMapping("/queryLogs")
    public ResponseBase queryLogs(@RequestParam("id") int id){
        try {
            Logs logs = logsService.queryLogsById(id);
            return ResponseBaseEntity.setResultSuccess(logs);
        }catch (Exception e){
            logger.error("查询日志详情失败! {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   查询日志分页列表
     * @date 2018/7/12 9:32
     * @param pageNum, pageSize
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/logs/queryLogsPage" ,logs = "日志分页查询")
    @GetMapping("/queryLogsPage")
    public ResponseBase queryLogsPage(@RequestParam("pageNum") int pageNum,
                                      @RequestParam("pageSize") int pageSize,
                                      @RequestParam("reverse") String reverse){
        try {
            PageInfo pageInfo = logsService.queryLogsPage(pageNum, pageSize ,reverse);
            return ResponseBaseEntity.setResultSuccess(pageInfo);
        }catch (Exception e){
            logger.error("查询日志分页列表失败! {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }
}
